package com.kelvin.oocl.transactionBlock.v1.impl;

import com.kelvin.oocl.transactionBlock.v1.MyTransaction;
import com.kelvin.oocl.testTransaction.tool.DBUtils;

import java.sql.Connection;
import java.sql.SQLException;

public abstract class MyTransactionBlock implements MyTransaction{
    private Connection connection;

    @Override
    public void execute() throws SQLException {
        initialize();
        this.run();
        this.commit();
    }

    protected abstract void run();

    private final void commit() throws SQLException {
        connection.commit();
    }

    private void initialize() throws SQLException {
        connection = DBUtils.getConnection();
        connection.setAutoCommit(false);
    }
}
